﻿/* 
 * Copyright 2010 Justin Pealing
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */

using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.Win32.SafeHandles;

namespace Minnow.Web.Native
{
    /// <summary>
    /// Process start information
    /// </summary>
    public class ProcessStartInfo
    {
        /// <summary>
        /// Initialises a new instance of <see cref="ProcessStartInfo" />
        /// </summary>
        public ProcessStartInfo()
        {
            this.RedirectStandardError = true;
            this.RedirectStandardInput = true;
            this.RedirectStandardOutput = true;
        }

        /// <summary>
        /// Gets dictionary of environment variables
        /// </summary>
        /// <remarks>By default the list of environment variables contains all of the environment variables
        /// for the current process.</remarks>
        public IDictionary<string, string> EnvironmentVariables
        {
            get
            {
                if (this.environmentVariables == null)
                {
                    this.environmentVariables = GetEnvironmentVariables();
                }
                return this.environmentVariables;
            }
        }
        private IDictionary<string, string> environmentVariables;

        /// <summary>
        /// Gets and sets the command line to be executed.
        /// </summary>
        public string CommandLine
        {
            get;
            set;
        }

        /// <summary>
        /// Gets and sets the path to the working directory for the process.
        /// </summary>
        /// <remarks>
        /// If this is null then the new process will have the same working directory as the parent process.
        /// </remarks>
        public string WorkingDirectory
        {
            get;
            set;
        }

        /// <summary>
        /// If true, indicates that the process is a console application that is being run without a console window.
        /// Therefore, the console handle for the application is not set.
        /// </summary>
        /// <remarks>
        /// This flag is ignored if the application is not a console application.
        /// </remarks>
        public bool CreateNoWindow
        {
            get;
            set;
        }

        /// <summary>
        /// If true indicates that the standard input for the process should be redirected using anonymous pipes.
        /// </summary>
        /// <remarks>
        /// This must be false in order to pass a handle as standard input to the process.
        /// Defaults to true.
        /// </remarks>
        public bool RedirectStandardInput
        {
            get;
            set;
        }

        /// <summary>
        /// If true indicates that the standard output for the process should be redirected using anonymous pipes.
        /// </summary>
        /// <remarks>
        /// This must be false in order to pass a handle as standard output to the process.
        /// Defaults to true.
        /// </remarks>
        public bool RedirectStandardOutput
        {
            get;
            set;
        }

        /// <summary>
        /// If true indicates that the standard error for the process should be redirected using anonymous pipes.
        /// </summary>
        /// <remarks>
        /// This must be false in order to pass a handle as standard error to the process.
        /// Defaults to true.
        /// </remarks>
        public bool RedirectStandardError
        {
            get;
            set;
        }

        // TODO: Understand safe handles
        public SafeFileHandle StandardInputHandle
        {
            get;
            set;
        }

        public SafeFileHandle StandardOutputHandle
        {
            get;
            set;
        }

        public SafeFileHandle StandardErrorHandle
        {
            get;
            set;
        }

        /// <summary>
        /// Gets the collection of environment variables of the current process.
        /// </summary>
        /// <returns>Collection of environment variables.</returns>
        private static IDictionary<string, string> GetEnvironmentVariables()
        {
            Dictionary<string, string> returnValue = new Dictionary<string, string>();

            foreach (DictionaryEntry variable in Environment.GetEnvironmentVariables())
            {
                returnValue.Add((string)variable.Key, (string)variable.Value);
            }

            return returnValue;
        }
    }
}